package com.pms.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.pms.entity.Data;
import com.pms.entity.Member;
import com.pms.entity.Project;
import com.pms.util.DBHelp;
import com.pms.util.DateUtil;
import com.pms.util.Page;
import com.pms.util.PkUtil;
import com.pms.util.RowMapper;

public class DataDao {
	
	DBHelp<Data> db = new DBHelp<Data>();
	
	public class DataRowMapper implements RowMapper<Data>{

		@Override
		public Data mapRow(ResultSet rs) throws SQLException {
			
			Data data = new Data();
			data.setId(rs.getString("id"));
			data.setTitle(rs.getString("title"));
			data.setDescribe(rs.getString("describe"));
			data.setContent(rs.getString("content"));
			data.setProjectId(rs.getString("t_project_id"));
			data.setMemberId(rs.getString("t_member_id"));
			data.setTime(rs.getString("time"));
			
			String projectId = rs.getString("t_project_id");
			String memberId = rs.getString("t_member_id");
			
			MemberDao memberDao = new MemberDao();
			Member member = memberDao.findById(memberId);
			data.setMember(member);
			
			ProjectDao projectDao = new ProjectDao();
			Project project = projectDao.findById(projectId);
			data.setProject(project);
			
			return data;
		}
		
	}
	public Long getCount(String projectId) {
		String sql = "select count(*) from t_data where t_project_id =?";
		return (Long) db.executeQueryForFunction(sql,projectId);
	}

	public Page<Data> findForList(String projectId ,Page page) {
		String sql="select id ,title,`describe`,content,`time`,t_member_id,t_project_id from t_data where t_project_id = ? order by time desc limit ?,?";
		 List<Data> list =db.executeQueryForList(sql, new DataRowMapper(), projectId,page.getOffSet(),page.getPageSize());
		 page.setResult(list);
		 
		return page;
		 
	}

	public Data findDataById(String dataId) {
		
		String sql ="select id ,title,`describe`,content,`time`,t_member_id,t_project_id from t_data where id = ?";
		return db.executeQueryForObject(sql, new DataRowMapper(),dataId);
	}

	public void editData(String title, String describe,String content, String memberId, String projectId, String dataId) {
		String sql ="update t_data set title=?, `describe`=?, content=?, time=?, t_member_id=?,t_project_id =? where id=?";
		db.executeSQL(sql,title,describe,content,DateUtil.getNow(),memberId,projectId,dataId);
	}

	public void deleteByDateId(String dataId) {
		String sql ="delete from t_data where id=?";
		db.executeSQL(sql, dataId);
		
	}

	public void newData(String title, String describe,String content, String memberId, String projectId) {
		String sql="INSERT INTO t_data(id,title,`describe`,`time`,content,t_member_id,t_project_id)VALUES(?,?,?,?,?,?,?);";
		
		String id = PkUtil.getPk();
		String time = DateUtil.getNow();
		db.executeSQL(sql,id, title,describe,time,content,memberId,projectId);
		
	}

	public Data findDateInformationById(String dataId) {
		String sql ="select id ,title,`describe`,content,`time`,t_member_id,t_project_id from t_data where id=?;";
		return db.executeQueryForObject(sql, new DataRowMapper(), dataId);
		
		
		
	}
}
